/**  
* @Title: HouseServiceImpl.java
* @Package com.house.service.impl
* @version V1.0  
*/
package com.hjb.service.impl.basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hjb.service.basic.HouseService;
import com.hjb.service.domain.House;
import com.hjb.service.domain.HouseImage;
import com.hjb.service.domain.ThouseRoom;
import com.hjb.service.mapper.HouseImageMapper;
import com.hjb.service.mapper.HouseMapper;
import com.hjb.service.mapper.ThouseRoomMapper;
import com.hjb.service.utils.NumberUtil;
import com.hjb.service.utils.StringUtil;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @ClassName: HouseServiceImpl
 * @Description: TODO(房源基本信息)
 * @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>
 * @date 2017年4月29日 下午2:33:35
 */
@Service
public class HouseServiceImpl implements HouseService {

	@Autowired
	private HouseMapper houseMapper;

	@Autowired
	private HouseImageMapper houseImageMapper;
	@Autowired
	private ThouseRoomMapper thouseRoomMapper;

	@Override
	public List<Map<String, Object>> getHouseList(HttpServletRequest request) {

		if (!StringUtil.isEmpty(request.getParameter("type"))) {
			int type = Integer.parseInt(request.getParameter("type"));
			switch (type) {
			case 1:
				System.out.println(1);
				break;
			case 2:
				// String[] value= request.getParameter("value").split("-");
				// return
				// houseMapper.initHomeByPrice(Integer.parseInt(value[0]),Integer.parseInt(value[1]));
			case 3:
				String[] value3 = request.getParameter("value").split("-");
				return houseMapper.initHomeByPrice(Integer.parseInt(value3[0]), Integer.parseInt(value3[1]));
			case 4:
				String value4 = request.getParameter("value");
				return houseMapper.initHomeSort(value4);
			case 5:
				String rentType = request.getParameter("rentType");
				String rentPay = request.getParameter("rentPay");
				String rentRoom = request.getParameter("rentRoom");
				return houseMapper.initHomeByOther(rentType, rentPay, rentRoom);
			default:
				break;
			}
		}

		return houseMapper.initHome();
	}

//	@Override
//	public House getHouseById(String houseId) {
//		House house = new House();
//		house.setId(houseId);
//
//		return houseMapper.selectByPrimaryKey(house);
//
//	}
	
	@Override
	public ThouseRoom getHouseById(String houseId) {
//		ThouseRoom house = new ThouseRoom();
//		house.setRoomid(houseId);
		
		Example example = new Example(ThouseRoom.class);
		example.createCriteria().andEqualTo("roomid",houseId);
		
		return thouseRoomMapper.selectByExample(example).get(0);
		
	}

	@Override
	public List<HouseImage> getHouseImageByHouseId(String houseId) {

		Example example = new Example(HouseImage.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("houseId", houseId);
		example.orderBy("mod_time").desc();

		List<HouseImage> list = houseImageMapper.selectByExample(example);

		criteria = null;
		example.clear();
		return list;
	}

	@Override
	public List<Map<String, Object>> getHouseFurniture(String houseId) {

		return houseMapper.getHouseFurniture(houseId);
	}

//	@SuppressWarnings("unused")
//	@Override
//	public Map<String,Object> indexHome(HttpServletRequest request) {
//		
//		Map<String,Object> resultMap = new HashMap<String,Object>();
//		
//		int showCount = NumberUtil.getInteger(request.getParameter("pageSize"), 10);
//		int currentPage = NumberUtil.getInteger(request.getParameter("pageNum"), 1);
//		Page page = PageHelper.startPage(currentPage, showCount, true);
//		
//		String key= request.getParameter("key");
//		String stext = request.getParameter("stext");
//		Example example = new Example(House.class);
//		if(key != null && !key.equals("0")){
//			example.createCriteria().andEqualTo(key, request.getParameter("value"));
//		}
//		
//		if(stext!=null && !stext.equals("")){
//			example.createCriteria().andLike("searchField", "%"+stext+"%");
//			resultMap.put("stext", stext);
//		}
//		
//		example.orderBy("mod_time").desc();
//		List<House> houseList = houseMapper.selectByExample(example);
//		resultMap.put("total", page.getTotal());
//		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
//		
//		for(House house: houseList){
//			Map<String,Object> hmap = new HashMap<String,Object>();
//			example = new Example(HouseImage.class);
//			Criteria criteria = example.createCriteria();
//			criteria.andEqualTo("houseId", house.getId());
//			example.orderBy("mod_time").desc();
//			List<HouseImage> houseImage = houseImageMapper.selectByExample(example);
//			hmap.put("house", house);
//			hmap.put("houseImage", houseImage);
//			result.add(hmap);
//		}
//		resultMap.put("data", result);
//		return resultMap;
//	}

	

	@SuppressWarnings("unused")
	@Override
	public Map<String,Object> indexHome(HttpServletRequest request) {
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		
		int showCount = NumberUtil.getInteger(request.getParameter("pageSize"), 10);
		int currentPage = NumberUtil.getInteger(request.getParameter("pageNum"), 1);
		Page page = PageHelper.startPage(currentPage, showCount, true);
		
		String key= request.getParameter("key");
		String stext = request.getParameter("stext");
		Example example = new Example(ThouseRoom.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("rentStatus",1);
		if(key != null && !key.equals("0")){
			criteria.andEqualTo(key, request.getParameter("value"));
		}
		
		if(stext!=null && !stext.equals("")){
			criteria.andLike("searchField", "%"+stext+"%");
			resultMap.put("stext", stext);
		}
		
		example.orderBy("room_mod_time").desc();
		List<ThouseRoom> houseList = thouseRoomMapper.selectByExample(example);
		resultMap.put("total", page.getTotal());
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		
		for(ThouseRoom house: houseList){
			Map<String,Object> hmap = new HashMap<String,Object>();
			example = new Example(HouseImage.class);
			Criteria criteria1 = example.createCriteria();
			criteria1.andEqualTo("houseId", house.getId());
			example.orderBy("mod_time").desc();
			List<HouseImage> houseImage = houseImageMapper.selectByExample(example);
			hmap.put("house", house);
			hmap.put("houseImage", houseImage);
			result.add(hmap);
		}
		resultMap.put("data", result);
		return resultMap;
	}

	@Override
	public List<Map<String, Object>> getHouseRoomFurniture(String houseId) {
	
		return thouseRoomMapper.getRoomFurniture(houseId);
	}

	@Override
	public List<ThouseRoom> getAllRoomByHouseId(String id) {
		
		Example example = new Example(ThouseRoom.class);
		example.orderBy("roomNo").asc();
		example.createCriteria().andEqualTo("id", id);
		
		return thouseRoomMapper.selectByExample(example);
	}
}
